package com.ruoyi.kjy.controller;

import java.util.Arrays;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.kjy.domain.Enterprise;
import com.ruoyi.kjy.service.IEnterpriseService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;

/**
 * 企业Controller
 * 
 * @author llr
 * @date 2023-04-27
 */
@RestController
@RequestMapping("/enterprise")
public class EnterpriseController extends BaseController
{
    @Autowired
    private RemoteUserService remoteUserService;
    @Autowired
    private IEnterpriseService enterpriseService;

    /**
     * 查询企业列表
     */
    @GetMapping("/list")
    public AjaxResult list(Integer current, Integer size,Enterprise enterprise)
    {
        if (Objects.isNull(current)) {
            current = 1;
        }
        if (Objects.isNull(size)) {
            size = 10;
        }
        if (size >= 20) {
            size = 20;
        }
        LambdaQueryWrapper<Enterprise> queryWrapper = new LambdaQueryWrapper<Enterprise>().setEntity(enterprise);
//        queryWrapper.or().like(enterprise.getName() != null, Enterprise::getName, enterprise.getName());
        Page<Enterprise> list = enterpriseService.page(new Page<>(current, size), queryWrapper);
        return AjaxResult.success(list);
    }


    /**
     * 获取企业详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(enterpriseService.getById(id));
    }

    /**
     * 新增企业
     */
    @Log(title = "企业", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Enterprise enterprise)
    {
        enterprise.setHightechEnterprises("0");
//        enterprise.setFunding(new BigDecimal(0));
        boolean save = enterpriseService.save(enterprise);
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        sysUser.setEnterpriseId(enterprise.getId());
        remoteUserService.updateInfo(sysUser);
        return toAjax(save);
    }

    /**
     * 修改企业
     */
    @Log(title = "企业", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Enterprise enterprise)
    {
        return toAjax(enterpriseService.updateById(enterprise));
    }

    /**
     * 删除企业
     */
    @Log(title = "企业", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(enterpriseService.removeByIds(Arrays.asList(ids)));
    }







}
